<link rel="stylesheet" href="${request.contextPath}/static/oms/default/plugins/zTree/css/zTreeStyle/zTreeStyle.css" type="text/css">
<div class="row">
	<div class="col-md-12">
		<div class="box-body  no-padding">
			<form id="update-from" class="form-horizontal" action="${request.contextPath}/system/authority/organization/update" method="post" onsubmit="return false">
				<input type="hidden" class="form-control" name="id"  id="sysOrganizationId"  value="${sysOrganization.id}" >
				<div class="form-group">
					<label id="sysOrganizationNameLabel" for="sysOrganizationName" class="col-sm-2 control-label">名称</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" name="name"  id="sysOrganizationName"  value="${sysOrganization.name}" placeholder="输入名称...">
					</div>
					<label id="sysOrganizationCodeLabel" for="sysOrganizationCode" class="col-sm-1 control-label" >编码</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" name="code" id="sysOrganizationCode" value="${sysOrganization.code}" placeholder="输入编码...">
					</div>
				</div>
				<div class="form-group">
					<label id="sysOrganizationParentIdLabel" for="sysOrganizationParentId" class="col-sm-2  control-label">上级机构</label>
					<div class="col-sm-9">
						<div class="input-group">
							<input type="hidden" class="form-control" id="sysOrganizationParentId" name="parentId"  value="${sysOrganization.parentId}" >
							<input type="text" class="form-control"  id="sysOrganizationParentName" value="" placeholder="选择上级机构...">
							<span class="input-group-addon"><i class="glyphicon glyphicon-remove" style="color: #FF0000;" id="sysOrganizationParentIdClear"></i></span>
							<div id="sysOrganizationContent" class="sysOrganizationContent" style="position: absolute; left: 59px; top: 25px; display: none;z-index: 999999;">
								<ul id="sysOrganizationParentTree" class="ztree" style="margin-top: 10px;
																border: 1px solid #d6e0df;
																background: #fbfbfb;
																width: 220px;
																height: 360px;
																overflow-y: scroll;
																overflow-x: auto; -moz-user-select: none;">
								</ul>
							</div>
						</div>
					</div>
				</div>
				<div class="form-group">
					<label id="sysOrganizationSeqLabel" for="sysOrganizationSeq" class="col-sm-2  control-label">排序</label>
					<div class="col-sm-4">
						<input class="form-control" name="seq" id="sysOrganizationSeq" type="number" value="${sysOrganization.seq}" placeholder="输入排序号...">
					</div>
					<label id="sysOrganizationStatusLabel" for="sysOrganizationStatus" class="col-sm-1  control-label">状态</label>
					<div class="col-sm-4">
						<select class="form-control" id="sysOrganizationStatus"  name="status"  value="${sysOrganization.status}" style="width: 100%;" placeholder="选择状态...">
						</select>
					</div>
				</div>
				<div class="form-group">
					<label id="sysOrganizationAddressLabel" for="sysOrganizationAddress" class="col-sm-2  control-label">机构地址</label>
					<div class="col-sm-4">
						<input type="text" class="form-control" id="sysOrganizationAddress" name="address"  value="${sysOrganization.address}"   placeholder="输入机构地址..." />
					</div>
					<label id="sysOrganizationTypeLabel" for="sysOrganizationType" class="col-sm-1  control-label">类型</label>
					<div class="col-sm-4">
						<select class="form-control" id="sysOrganizationType" name="type"  value="${sysOrganization.type}" style="width: 100%;" placeholder="选择机构类型...">
						</select>
					</div>
				</div>
				<div class="form-group">
					<label id="sysOrganizationDescriptionLabel" class="col-sm-2 control-label" >描述</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" name="description" id="sysOrganizationDescription"  value="${sysOrganization.description}" placeholder="输入描述...">
					</div>
				</div>
				<div class="form-group">
					<label id="sysOrganizationRemarkLabel" class="col-sm-2 control-label">备注</label>
					<div class="col-sm-9">
						<input type="text" class="form-control" name="remark" id="sysOrganizationRemark"  value="${sysOrganization.remark}" placeholder="输入备注...">
					</div>
				</div>
				<div class="error-message text-center" >
					<span class="error-icon"></span>
					<span id="error-msg" class="error-msg" ></span>
				</div>
				<div class="box-footer">
					<div class="pull-right">
						<button type="submit" class="btn btn-primary btn-sm" ><i class="fa fa-save"></i> 保存</button>
						<button type="button" class="btn btn-default btn-sm" id="close" data-dismiss="modal"><i class="fa fa-close"></i>关闭</button>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<script type="text/javascript" src="${request.contextPath}/static/oms/default/plugins/zTree/jquery.ztree.all.js"></script>
<script type="text/javascript">
    var setting = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "parentId",
                rootPId: null
            }
        },
        async: {
            enable: true,
            url:"/system/authority/organization/treeGrid"
        },
        callback:{
            onAsyncSuccess:function (event, treeId) {
                var node = $.fn.zTree.getZTreeObj(treeId).getNodeByParam("id", $("#sysOrganizationParentId").val());
                if(node){
                    $("#sysOrganizationParentName").val(node.name);
                }
            },
            onClick :function (event, treeId, treeNode) {
				$("#sysOrganizationParentId").val(treeNode.id);
				$("#sysOrganizationParentName").val(treeNode.name);
                $('#sysOrganizationContent').hide();
            }
        }
    };

	$(function() {
		$("#sysOrganizationType").select2({
			data: OrganizationTypeStore,
            language: "zh-CN",
            theme: "bootstrap"
		});
        $("#sysOrganizationType").select2('val','${sysOrganization.type}');


		$("#sysOrganizationStatus").select2({
			data: StatusStore,
			language: "zh-CN",
            theme: "bootstrap"
		});
        $("#sysOrganizationStatus").select2("val",'${sysOrganization.status}');

        $.fn.zTree.init($('#sysOrganizationParentTree'), setting);

        $("#sysOrganizationParentName").on('click',function () {
            $('#sysOrganizationContent').css("display")==='none'?$('#sysOrganizationContent').show():$('#sysOrganizationContent').hide();
        });
        $("#sysOrganizationContent").mouseleave(function () {
            $('#sysOrganizationContent').hide();
        });

        $("#sysOrganizationParentIdClear").on('click',function () {
            $("#sysOrganizationParentId").val("");
            $("#sysOrganizationParentName").val("");
        });



        function ajaxSubmit(form) {

            var $form = $(form);

            if ($form.length === 1 && $form.validate()) {
                $.post(form.action, $form.serialize(),
                    function(result) {
                        if (result.success) {
                            $("#lgModal").modal('hide');
                            alertMsg(result.message,"success");
                            window.location.reload();
                        } else {
                            $form.find(".error-msg").show();
                            $form.find(".error-msg").html(result.message);
                        }
                    }, 'json');
            }
        }

        var $updateForm = $("#update-from");
        $updateForm.validate({
            rules: {
                name: {
                    required: true
                },
                code: {
                    required: true
                },
                seq: {
                    required: true
                }
            },
            messages: {
                name:{
                    required:"名称不能为空！"
                },
                code:{
                    required:"编码不能为空！"
                },
                seq: {
                    required: "排序号不能为空！"
                }
            },
            submitHandler:ajaxSubmit,
            errorLabelContainer:".error-msg",
            wrapper:"li",
            showErrors:function(errorMap,errorList) {
                if(errorList.length>0){
                    $updateForm.find(".error-msg").html(errorList[0].message);
                }else{
                    $updateForm.find(".error-msg").html("");
                }
            },
            onkeyup: false
        });
    });
</script>
